<!-- 行政班级-班级管理 -->
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" href="/css/font.css">
		<link rel="stylesheet" href="/css/xadmin.css">
	</head>

	<body>
		<div class="x-body" id="app">
			<xblock class="layui-form">
				<button class="layui-btn layui-btn-danger" onclick="removeAll()"><i class="layui-icon"></i>批量移除</button>
				<button class="layui-btn" onclick="open_addStu()" ><i class="layui-icon"></i>添加学生</button>
				<input type="hidden" th:attr="majorNo=${majorNo},classId=${classId}" id="input_majorNo"/>
				<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);"
                title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
				<span class="x-right" style="line-height:40px">共有学生：{{ pageInfo.total }} 人，当前{{ pageInfo.pageNum }} / {{ pageInfo.pages }}页</span>
				<form id="import_excel_form" enctype="multipart/form-data">
                   <input type="file" hidden="hidden" name="excelFile" id="importInput" onchange="import_excel(this)" accept=".xlsx"/>
                </form>
			</xblock>
			<table class="layui-table">
				<thead>
					<tr>
						<th width="20">
							<div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
						</th>
						<th>学号</th>
						<th>姓名</th>
						<th width="50">性别</th>
						<th width="100">出生日期</th>
						<th width="100">籍贯</th>
						<th width="100">专业</th>
						<th width="150">操作</th>
				</thead>
				<tbody>
					<tr v-for="student in students">
                        <td>
                            <div class="layui-unselect layui-form-checkbox" lay-skin="primary" :data-id='student.stuId' :data-stuName="student.stuName"><i class="layui-icon">&#xe605;</i></div>
                        </td>
                        <td>{{ student.stuNo }}</td>
                        <td>{{ student.stuName }}</td>
                        <td v-if="student.stuSex == 2">男</td>
                        <td v-else-if="student.stuSex == 1">女</td>
                        <td>{{ student.stuBirth }}</td>
                        <td>{{ student.homeplace }}</td>
                        <td>{{ student.majorNo }}</td>
                        <td class="td-manage">
                            <button class="layui-btn-danger layui-btn layui-btn-sm" onclick="member_del(this)" href="javascript:;"><i
                                 class="layui-icon">&#xe640;</i>移除</button>
                        </td>
                    </tr>
				</tbody>
			</table>
			<div class="page" style="margin-top: 10px !important;">
               <div>
                 <a class="prev pageTxt" href="javascript:void(0)">上一页</a>
                 <a class="first pageTxt" href="javascript:void(0)">首页</a>
                 <template v-for="pageNum in pageInfo.navigatepageNums">
                     <a class="num" href="javascript:void(0)" v-if="pageInfo.pageNum != pageNum" :data="pageNum">{{ pageNum }}</a>
                     <span class="current" v-else :data="pageNum">{{ pageNum }}</span>
                 </template>
                 <a class="last pageTxt" href="javascript:void(0)">尾页</a>
                 <a class="next pageTxt" href="javascript:void(0)">下一页</a>
               </div>
             </div>
		</div>
		<script type="text/javascript" src="/js/jquery.min.js"></script>
        <script type="text/javascript" src="/js/xadmin.js"></script>
        <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="/js/vue.min.js"></script>
        <script type="text/javascript" src="/js/jquery.form.js"></script>
		<script>
			var vm = new Vue({
	            el : '#app',
	            data : {
	                // 当前班级的学生列表
	                students : "",
	                pageInfo:"",
	                // 当前专业代码
	                majorNo:"",
	                // 当前班级id
	                classId : ""
	            },
	            created : function() {
	                this.majorNo = $("#input_majorNo").attr("majorNo");
	                this.classId = $("#input_majorNo").attr("classId");
	                // 获取当前班级的学生列表
	             this.listStudentByClassId(this.classId, 1);
	            },
	            methods : {
	                listStudentByClassId : function(classId, pageNum) {
	                    var vm = this;
	                    $.ajax({
	                        url : "/sys/class/as/listStudentByClassId",
	                        data: {
	                        	classId: classId,
	                        	pageNum: pageNum
	                        },
	                        type : "get",
	                        success : function(result) {
	                            if (result.state) {
	                                vm.pageInfo = result.data;
	                                vm.students = result.data.list;
	                            } else {
	                                layui.use(['layer'], function(){
	                                    var layer = layui.layer
	                                    layer.msg(""+result.msg, {icon: 5});
	                                });
	                            }
	                        }
	                    });
	                }
	            }
	        });
		
			// 点击页码数，进行跳转
            $(".page").delegate(".num", "click", function() {
                var pageNum = $(this).attr("data");
                vm.listStudentByClassId(vm.classId, pageNum);
            });
            // 上一页
            $(".page").delegate(".prev", "click", function() {
                if (vm.pageInfo.hasPreviousPage) {
                    vm.listStudentByClassId(vm.classId, vm.pageInfo.pageNum - 1);
                }
            });
            // 下一页
            $(".page").delegate(".next", "click", function() {
                if (vm.pageInfo.hasNextPage) {
                    vm.listStudentByClassId(vm.classId, vm.pageInfo.pageNum + 1);
                }
            });
            // 首页
            $(".page").delegate(".first", "click", function() {
                if (!vm.pageInfo.isFirstPage) {
                    vm.listStudentByClassId(vm.classId, 1);
                }
            });
            // 尾页
            $(".page").delegate(".last", "click", function() {
                if (!vm.pageInfo.isLastPage) {
                    vm.listStudentByClassId(vm.classId, vm.pageInfo.pages);
                }
            });
			
	        // 打开添加学生的页面
			function open_addStu() {
			    x_admin_show('添加学生','/sys/class/as/toAsClassAddStu?majorNo=' + vm.majorNo + "&classId=" + vm.classId, 600, 400);
			}
			// 单个移除
			function member_del(obj) {
				var stuName = $(obj).parents("tr").find("div:first").attr("data-stuName");
				var id = $(obj).parents("tr").find("div:first").attr("data-id");
				var stuId = [];
				stuId.push(id);
				layer.confirm('确认要移除【' + stuName + '】吗？', function(index) {
					$.ajax({
						url: "/sys/class/as/delStuFromClass",
						type: "post",
						data: {
							stuId: stuId
						},
						success: function(result) {
							if (result.state) {
								layer.msg('移除成功!', {
									icon: 1,
									time: 1000
								});

								// 根据当前页面数据量决定加载前一页还是当前页
								if (vm.pageInfo.size - 1 == 0) {
									vm.listStudentByClassId(vm.classId, vm.pageInfo.pageNum - 1);
								} else {
									vm.listStudentByClassId(vm.classId, vm.pageInfo.pageNum);
								}
							} else {
								layer.msg("" + result.msg, {
									icon: 5
								});
							}
						}
					});
				});
			}

			// 批量移除
			function removeAll() {
				var stuId = tableCheck.getData();
				if (stuId.length <= 0) {
					return;
				}
				// 获取选中的学生姓名
				var obj = $(".layui-form-checked").not('.header');
				var stuName = [];
				obj.each(function(index, el) {
					stuName.push(obj.eq(index).attr('data-stuName'));
				});
				layer.confirm('确认要移除【' + stuName + '】吗？', function(index) {
					$.ajax({
						url: "/sys/class/as/delStuFromClass",
						type: "post",
						data: {
							stuId: stuId
						},
						success: function(result) {
							if (result.state) {
								layer.msg('移除成功', {
									icon: 1
								});
								// 移除所有checkbox的勾选状态
								$(".layui-form-checkbox").removeClass('layui-form-checked');
								// 根据当前页面数据量决定加载前一页还是当前页
								if (vm.pageInfo.size - stuId.length == 0  && vm.pageInfo.pageNum != 1) {
									vm.listStudentByClassId(vm.classId, vm.pageInfo.pageNum - 1);
								} else {
									vm.listStudentByClassId(vm.classId, vm.pageInfo.pageNum);
								}
							} else {
								layer.msg("" + result.msg, {
									icon: 5
								});
							}
						}
					});
				});
			}
			
			
			
			
			 
			  
            
	        // 触发input文件上传
            function open_inputFile() {
                $("#importInput").click();
            }
            // 导入数据(excel格式)
            function import_excel(obj) {
                var file = $(obj).val();
                // 当所选文件不为空时
                if (file.length != 0) {
                    $("#import_excel_form").ajaxSubmit({
                            type:'post',
                            url:'/sys/class/as/importStuToClass',
                            success:function(result){
                                if (result.state) {
                                    layer.msg("导入数据成功", {icon: 1},function(){
                                        // 刷新页面
                                        location.reload();
                                    });
                                } else {
                                    layer.msg(""+result.msg, {icon: 5});
                                }
                                // 重置input中的文件
                                document.getElementById("import_excel_form").reset();
                            }
                    });
                }
            }
	        
		</script>
	</body>

</html>
